package com.glacier.common.core.domain;

import com.glacier.common.core.constant.HttpStatusEnum;

import java.io.Serializable;
import java.time.Instant;

/**
 * date 2022-11-06 21:07
 * 响应体
 *
 * @author glacier
 * @version 1.0
 */
public class Result<T> implements Serializable {
    private static final long serialVersionUID = 1531212462658710850L;
    /**
     * 时间戳
     */
    private final Instant time;
    /**
     * 编码 默认成功
     */
    private String code;
    /**
     * 总记录
     */
    private long count;
    /**
     * 信息
     */
    private String msg;
    /**
     * 数据
     */
    private T data;

    public Result() {
        this(HttpStatusEnum.SUCCESS.getCode(), HttpStatusEnum.SUCCESS.getName(), null);
    }

    public Result(String code, String msg, T data) {
        this(code, msg, data, 0);
    }

    public Result(String code, String msg, T data, long count) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        this.count = count;
        this.time = Instant.now();
    }

    public static <T> Result<T> ok(T data) {
        return ok(HttpStatusEnum.SUCCESS.getName(), data);
    }

    public static <T> Result<T> ok(String msg, T data) {
        return new Result<>(HttpStatusEnum.SUCCESS.getCode(), msg, data);
    }

    public static <T> Result<T> ok(String msg, T data, long count) {
        return new Result<>(HttpStatusEnum.SUCCESS.getCode(), msg, data, count);
    }

    public static <T> Result<T> fail(String msg) {
        return new Result<>(HttpStatusEnum.SYSTEM_ERROR.getCode(), msg, null);
    }

    public static <T> Result<T> fail(String code, String msg) {
        return new Result<>(code, msg, null);
    }

    public String getCode() {
        return code;
    }

    public Result<T> setCode(String code) {
        this.code = code;
        return this;
    }

    public String getMsg() {
        return msg;
    }

    public Result<T> setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public Instant getTime() {
        return time;
    }

    public long getCount() {
        return count;
    }

    public void setCount(long count) {
        this.count = count;
    }

    public T getData() {
        return data;
    }

    public Result<T> setData(T data) {
        this.data = data;
        return this;
    }

    @Override
    public String toString() {
        return "Result{" +
                "time=" + time +
                ", code='" + code + '\'' +
                ", count='" + count + '\'' +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }
}
